package org.qnit.common;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    //milvus连接信息
    @Value("${spring.data.milvus.host}")
    private String host;
    @Value("${spring.data.milvus.port}")
    private int port;

    //mysql datasource config
    @Bean(name = "mysqlHikariConfig")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public HikariConfig mysqlHikariConfig() {
        return new HikariConfig();
    }

    @Bean(name = "mysqlDataSource")
    public DataSource mysqlDataSource(
            @Autowired @Qualifier("mysqlHikariConfig") HikariConfig hikariConfig) { //默认按类型匹配
        return new HikariDataSource(hikariConfig);
    }

    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate mysqlJdbcTemplate(
            @Autowired @Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean("milvusTemplate")
    public MilvusTemplate milvusTemplate() {
        return new MilvusTemplate(host, port);
    }

}
